//
//  ZREmoticonBar.swift
//  Sina
//
//  Created by 张锐 on 16/3/8.
//  Copyright © 2016年 zhangr. All rights reserved.
//

import UIKit

protocol ZREmoticonBarDelegate : NSObjectProtocol{

    /**
     Bar按钮点击
     */
    func emoticonBarButtonClick(button : UIButton)
}

class ZREmoticonBar: UIView {

    weak var delegate : ZREmoticonBarDelegate?
    
    //构造方法
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //setupUI
    
    private func setupUI(){
    
        addSubview(recentButton)
        
        addSubview(defaultButton)
        
        addSubview(emojiButton)
        
        addSubview(lxhButton)
        
        
        recentButton.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(self.snp_top)
            
            make.bottom.equalTo(self.snp_bottom)
            
            make.left.equalTo(self.snp_left)
            
            make.width.equalTo(defaultButton.snp_width)
            
        }
        
        defaultButton.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(self.snp_top)
            
            make.bottom.equalTo(self.snp_bottom)
            
            make.left.equalTo(recentButton.snp_right)
            
            make.width.equalTo(emojiButton.snp_width)
        }
        emojiButton.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(self.snp_top)
            
            make.bottom.equalTo(self.snp_bottom)
            
            make.left.equalTo(defaultButton.snp_right)
            
            make.width.equalTo(lxhButton.snp_width)
        }
        
        lxhButton.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(self.snp_top)
            
            make.bottom.equalTo(self.snp_bottom)
            
            make.left.equalTo(emojiButton.snp_right)
            
            make.right.equalTo(self.snp_right)
        }
    
    }
    //按钮点击事件
    func changeEmoticon(button : UIButton){
        
        for btn in subviews{
            
            let button = btn as! UIButton
            
            button.selected = false
        }
        button.selected = true
        
        //代理调用方法
        if (delegate != nil) {
        
            delegate?.emoticonBarButtonClick(button)
        
        }
        
    }
    //懒加载
    private lazy var recentButton : UIButton = {
    
        let button  = UIButton()
        
        button.setTitle("最近", forState: UIControlState.Normal)
        
        button.tag = 0
        
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_normal"), forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_selected"), forState: UIControlState.Selected)
        
        button.addTarget(self, action: "changeEmoticon:", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    
    }()
    
    private lazy var defaultButton : UIButton = {
        
        let button  = UIButton()
        
        button.setTitle("默认", forState: UIControlState.Normal)
        
        button.tag = 1
        
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_normal"), forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_selected"), forState: UIControlState.Selected)
        
        button.addTarget(self, action: "changeEmoticon:", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
        
    }()
    
    private lazy var emojiButton : UIButton = {
        
        let button  = UIButton()
        
        button.setTitle("emoji", forState: UIControlState.Normal)
        
        button.tag = 2
        
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_normal"), forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_selected"), forState: UIControlState.Selected)
        
        button.addTarget(self, action: "changeEmoticon:", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
        
    }()
    
    private lazy var lxhButton : UIButton = {
        
        let button  = UIButton()
        
        button.setTitle("浪小花", forState: UIControlState.Normal)
        
        button.tag = 3
        
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_normal"), forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_selected"), forState: UIControlState.Selected)
        
        button.addTarget(self, action: "changeEmoticon:", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
        
    }()

}
